#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main() {
    // 打开摄像头
    VideoCapture cap(0);
    if (!cap.isOpened()) {
        cerr << "Error opening the camera" << endl;
        return -1;
    }

    // 读取摄像头中的图像
    Mat frame;
    cap >> frame;

    // 创建灰度图像
    Mat gray;
    cvtColor(frame, gray, COLOR_BGR2GRAY);

    // 检测边缘
    Mat edges;
    Canny(gray, edges, 50, 150, 3);

    // 检测直线
    vector<Vec4i> lines;
    HoughLinesP(edges, lines, 1, CV_PI/180, 50, 50, 10);

    // 画出检测到的直线
    for (size_t i = 0; i < lines.size(); i++) {
        Vec4i l = lines[i];
        line(frame, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 3, LINE_AA);
    }

    // 显示结果
    imshow("Original", frame);
    imshow("Edges", edges);
    waitKey(0);

    return 0;
}
